﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using moose.extension;
using moose.utility;

namespace moose.client.extensionmanager
{
    /// <summary>
    /// Form used to browse Extensions
    /// </summary>
    public partial class ExtensionBrowserForm : Form
    {
        #region [Local Attributes]

        /// <summary>
        /// Collection of Extensions
        /// </summary>
        Dictionary<string, Assembly> m_dictExtensions;

        //TODO: Generate a matrix of references, so we can explore extensions and actions...

        #endregion

        #region [Construction]

        /// <summary>
        /// Initialize Extension Browser Form
        /// </summary>
        public ExtensionBrowserForm()
        {
            InitializeComponent();

            if (!DesignMode)
            {
                m_dictExtensions = new Dictionary<string, Assembly>();
                get_extensions();

                //TODO: Fill in the tree
            }

            return;
        }

        #endregion

        #region [Helper Methods]

        private void get_extensions()
        {
            Assembly asm;
            object[] attributes;
            ExtensionAttribute attrib;
            foreach (string extension_path in Directory.GetFiles(Environment.CurrentDirectory, "*.dll"))
            {
                asm = Assembly.LoadFile(extension_path);
                attributes = asm.GetCustomAttributes(typeof(ExtensionAttribute), false);
                if (attributes.Length == 1)
                {
                    attrib = (ExtensionAttribute)attributes[0];
                    m_dictExtensions[attrib.Name] = asm;
                }
            }

            return;
        }

        #endregion

    }
}
